# Create your views here.
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.template import Context
from django.template.loader import get_template
from django.contrib.auth.models import User
from django.shortcuts import render_to_response, render
from django import forms
from django.contrib.auth.forms import UserCreationForm
from ultimateImdbApp.models import Film,Director,Actor,Qualification
from django.utils import simplejson
from django.core import serializers

def mainpage(request):
	return render_to_response(
		'mainpage.html',
		{
			'titlehead': 'The Most Fucking Awesome Imdb of the History',
			'pagetitle': 'Welcome to the TMFAIH',
			'contentbody': 'If you are here, you are the Big Boss.',
			'user':request.user,
		})
	#template = get_template('mainpage.html')
	#variables = Context({
	#	'titlehead': 'The Most Fucking Awesome Imdb of the History',
	#	'pagetitle': 'Welcome to the TMFAIH',
	#	'contentbody': 'If you are here, you are the Big Boss.',
	#	'user':request.user,
	#})
	#output = template.render(variables)
	#return HttpResponse(output)

def userpage(request, idRef):
	try:
		user = User.objects.get(id=idRef)
		userS = User.objects.filter(id=idRef)
	except:
		raise Http404('User not found.')

	if request.META["QUERY_STRING"] == "json":
		data = serializers.serialize("json", userS)
		return HttpResponse(data, mimetype='application/json')
	if request.META["QUERY_STRING"] == "xml":
		data = serializers.serialize("xml", userS)
		return HttpResponse(data, mimetype='application/xml')

	qualifications = user.qualification_set.all()
	template = get_template('userpage.html')
	variables = Context({
		'username': user.username,
		'qualifications': qualifications
	})
	output = template.render(variables)
	return HttpResponse(output)

def users(request):
	if request.META["QUERY_STRING"] == "json":
		data = serializers.serialize("json", User.objects.all())
		return HttpResponse(data, mimetype='application/json')
	if request.META["QUERY_STRING"] == "xml":
		data = serializers.serialize("xml", User.objects.all())
		return HttpResponse(data, mimetype='application/xml')
	return render_to_response(
		'users.html',
		{
			'users': User.objects.all(),
		})

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            return HttpResponseRedirect("/mainpage/")
    else:
        form = UserCreationForm()
    return render(request, "registration/register.html", {
        'form': form,
    })

def actorspage(request):
	if request.META["QUERY_STRING"] == "json":
		data = serializers.serialize("json", Actor.objects.all())
		return HttpResponse(data, mimetype='application/json')
	if request.META["QUERY_STRING"] == "xml":
		data = serializers.serialize("xml", Actor.objects.all())
		return HttpResponse(data, mimetype='application/xml')

	return render_to_response(
		'actors.html',
		{
			'actors': Actor.objects.all(),
		})

def directorspage(request):
	if request.META["QUERY_STRING"] == "json":
		data = serializers.serialize("json", Director.objects.all())
		return HttpResponse(data, mimetype='application/json')
	if request.META["QUERY_STRING"] == "xml":
		data = serializers.serialize("xml", Director.objects.all())
		return HttpResponse(data, mimetype='application/xml')
	return render_to_response(
		'directors.html',
		{
			'directors': Director.objects.all(),
		})

def filmspage(request):
	if request.META["QUERY_STRING"] == "json":
		data = serializers.serialize("json", Film.objects.all())
		return HttpResponse(data, mimetype='application/json')
	if request.META["QUERY_STRING"] == "xml":
		data = serializers.serialize("xml", Film.objects.all())
		return HttpResponse(data, mimetype='application/xml')

	return render_to_response(
		'films.html',
		{
			'films': Film.objects.all(),
		})

def actorpage(request, actorRef):
	try:
		actor = Actor.objects.get(id=actorRef)
		actorS = Actor.objects.filter(id=actorRef)
	except:
		raise Http404('Actor not found.')

	if request.META["QUERY_STRING"] == "json":
		data = serializers.serialize("json", actorS)
		return HttpResponse(data, mimetype='application/json')
	if request.META["QUERY_STRING"] == "xml":
		data = serializers.serialize("xml", actorS)
		return HttpResponse(data, mimetype='application/xml')

	return render_to_response(
		'actor.html',
		{
			'actor': actor,
		})

def filmpage(request, filmRef):
	try:
		film = Film.objects.get(id=filmRef)
		filmS = Film.objects.filter(id=filmRef)
	except:
		raise Http404('Film not found.')

	if request.META["QUERY_STRING"] == "json":
		data = serializers.serialize("json", filmS)
		return HttpResponse(data, mimetype='application/json')
	if request.META["QUERY_STRING"] == "xml":
		data = serializers.serialize("xml", filmS)
		return HttpResponse(data, mimetype='application/xml')

	return render_to_response(
		'film.html',
		{
			'film': film,
		})

def directorpage(request, directorRef):
	try:
		director = Director.objects.get(id=directorRef)
		directorS = Director.objects.filter(id=directorRef)
	except:
		raise Http404('Director not found.')

	if request.META["QUERY_STRING"] == "json":
		data = serializers.serialize("json", directorS)
		return HttpResponse(data, mimetype='application/json')
	if request.META["QUERY_STRING"] == "xml":
		data = serializers.serialize("xml", directorS)
		return HttpResponse(data, mimetype='application/xml')

	return render_to_response(
		'director.html',
		{
			'director': director,
		})

#def addcoment(request):


def qualifications(request, filmRef):
	try:
		qualifications = Qualification.objects.filter(film=filmRef)
	except:
		raise Http404('Qualifications not found.')

	if request.META["QUERY_STRING"] == "json":
		data = serializers.serialize("json", qualifications)
		return HttpResponse(data, mimetype='application/json')
	if request.META["QUERY_STRING"] == "xml":
		data = serializers.serialize("xml", qualifications)
		return HttpResponse(data, mimetype='application/xml')

	return render_to_response(
		'qualifications.html',
		{
			'qualifications': qualifications,
			'film': Film.objects.get(id=filmRef)
		})

def qualification(request, filmRef, userRef):
	try:
		qualification = Qualification.objects.get(film=filmRef, user=userRef)
		qualificationS = Qualification.objects.filter(film=filmRef, user=userRef)
	except:
		raise Http404('Qualifications not found.')

	if request.META["QUERY_STRING"] == "json":
		data = serializers.serialize("json", qualificationS)
		return HttpResponse(data, mimetype='application/json')
	if request.META["QUERY_STRING"] == "xml":
		data = serializers.serialize("xml", qualificationS)
		return HttpResponse(data, mimetype='application/xml')

	return render_to_response(
		'qualification.html',
		{
			'qualification':qualification
		})
